<?php

namespace App\Http\Controllers\Backend;


use App\Manage\CourseCycleManage;
use App\Models\Course;
use App\Models\User;

class CourseCycleController extends BaseController
{

    // 课程上课时间
    public function initialize()
    {
        parent::initialize();

        $this->model = 'CourseCycle';
    }


    // 列表
    // 添加
    // 修改
    // 删除

    // 返回列表数据
    public function lists()
    {
        $manage = new CourseCycleManage();
        $result = $manage->onPageLists();
        if($result === false){
            return $this->error($manage->getError());
        }

        // 获取用户信息
        $uids  = array_column($result['lists'], 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);
        // 课程信息
        $cids    = array_column($result['lists'], 'course_id');
        $courses = (new Course())->isCache(true)->getListInIds($cids);
        foreach($result['lists'] as $key => $item){
            $result['lists'][$key]['user']   = $users[$item['uid']] ?? [];
            $result['lists'][$key]['course'] = $courses[$item['course_id']] ?? [];
        }

        return $this->success($result);
    }


    // 详情：修改的时候要使用的
    public function show()
    {
        $manage = new CourseCycleManage();
        $info   = $manage->onShow();
        if($info === false){
            return $this->error($manage->getError());
        }

        // 获取用户信息
        $info['user']   = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);
        $info['course'] = (new Course())->isCache(true)->getOneById($info['course_id'] ?? 0);

        return $this->success($info);
    }


}


